﻿@model PluginSearchModel
@using Nop.Services.Plugins

@{
    //page title
    ViewBag.PageTitle = T("Admin.Configuration.Plugins.Local").Text;
    //active menu item (system name)
    Html.SetActiveMenuItemSystemName("Local plugins");
}

@{
    const string hideSearchBlockAttributeName = "LocaLPluginsPage.HideSearchBlock";
    var hideSearchBlock = genericAttributeService.GetAttribute<bool>(workContext.CurrentCustomer, hideSearchBlockAttributeName);
}

<form asp-controller="Plugin" asp-action="List" id="plugins-form-local">
    <div class="content-header clearfix">
        <h1 class="pull-left">
            @T("Admin.Configuration.Plugins.Local")
        </h1>
        <div class="pull-right">
            @if (Model.NeedToRestart)
            {
                <button type="submit" name="plugin-apply-changes" class="btn bg-green">@T("Admin.Configuration.Plugins.ApplyChanges")</button>
                <button type="submit" name="plugin-discard-changes" class="btn bg-red">@T("Admin.Configuration.Plugins.DiscardChanges")</button>
            }

            <button type="submit" name="plugin-reload-grid" class="btn bg-blue">@T("Admin.Configuration.Plugins.ReloadList")</button>
            <script>
                $(document).ready(function() {
                    $("button[name='plugin-reload-grid']").on("click",
                        function(e) {
                            showThrobber(
                                '@Html.Raw(JavaScriptEncoder.Default.Encode(T("Admin.Configuration.Plugins.ReloadList.Progress").Text))');
                        });
                    $("button[name='plugin-apply-changes']").on("click",
                        function(e) {
                            showThrobber(
                                '@Html.Raw(JavaScriptEncoder.Default.Encode(T("Admin.Configuration.Plugins.ApplyChanges.Progress").Text))');
                        });
                    $("button[name='plugin-discard-changes']").on("click",
                        function(e) {
                            showThrobber(
                                '@Html.Raw(JavaScriptEncoder.Default.Encode(T("Admin.Configuration.Plugins.DiscardChanges.Progress").Text))');
                        });
                });
            </script>
            <button type="button" name="uploadplugin" class="btn bg-olive" data-toggle="modal" data-target="#uploadplugin-window">
                <i class="fa fa-upload"></i>
                @T("Admin.Configuration.Plugins.Upload")
            </button>
            @await Component.InvokeAsync("AdminWidget", new { widgetZone = AdminWidgetZones.PluginListButtons })
        </div>
    </div>

    <div class="content">
        <div class="form-horizontal">
            <script>
                $(document).ready(function () {
                    //install a plugin
                    $("#plugins-local-grid").on("click", ".install-plugin-link", function (e) {
                        $("html, body").animate({ scrollTop: 0 }, 400);
                        showThrobber('@Html.Raw(JavaScriptEncoder.Default.Encode(T("Admin.Configuration.Plugins.Fields.Install.Progress").Text))');
                        return true;
                    });

                    //uninstall a plugin
                    $("#plugins-local-grid").on("click", ".uninstall-plugin-link", function (e) {
                        $("html, body").animate({ scrollTop: 0 }, 400);
                        showThrobber('@Html.Raw(JavaScriptEncoder.Default.Encode(T("Admin.Configuration.Plugins.Fields.Uninstall.Progress").Text))');
                        return true;
                    });

                    //upload a plugin
                    $("#upload-plugin").on("click", function (e) {
                        $("html, body").animate({ scrollTop: 0 }, 400);
                        showThrobber('@Html.Raw(JavaScriptEncoder.Default.Encode(T("Admin.Configuration.Plugins.Upload.Progress").Text))');
                        return true;
                    });

                    //delete a plugin
                    $("#plugins-local-grid")
                        .on("click", ".delete-plugin-link", function (e) {
                            $(this)
                                .attr("data-toggle", "modal")
                                .attr("data-target", "#plugin-delete-confirmation");
                            $("#plugin-delete-confirmation button[type=submit]").attr("name", $(this).attr("name"));
                    });

                    $("#plugin-delete-confirmation button[type=submit]").on("click",
                        function() {
                            $('#plugin-delete-confirmation').modal('toggle');
                            $("html, body").animate({ scrollTop: 0 }, 400);
                            showThrobber('@Html.Raw(JavaScriptEncoder.Default.Encode(T("Admin.Configuration.Plugins.Fields.Delete.Progress").Text))');
                            return true;
                        });
                });
            </script>

            <div class="panel-group">
                <div class="panel panel-default panel-search">
                    <div class="panel-body">
                        <div class="row search-row @(!hideSearchBlock ? "opened" : "")" data-hideAttribute="@hideSearchBlockAttributeName">
                            <div class="search-text">@T("Admin.Common.Search")</div>
                            <div class="icon-search"><i class="fa fa-search" aria-hidden="true"></i></div>
                            <div class="icon-collapse"><i class="fa fa-angle-down" aria-hidden="true"></i></div>
                        </div>

                        <div class="search-body @(hideSearchBlock ? "closed" : "")">
                            <p>
                                <h4>@T("Admin.Configuration.Plugins.Description")</h4>
                                <ol>
                                    <li>@T("Admin.Configuration.Plugins.Description.Step1")</li>
                                    <li>@T("Admin.Configuration.Plugins.Description.Step2")</li>
                                    <li>@T("Admin.Configuration.Plugins.Description.Step3")</li>
                                    <li>@T("Admin.Configuration.Plugins.Description.Step4")</li>
                                    <li>@T("Admin.Configuration.Plugins.Description.Step5")</li>
                                </ol>
                            </p>
                            <p>
                                @T("Admin.Configuration.Plugins.Description.DownloadMorePlugins")
                            </p>
                            <div class="row">
                                <div class="col-md-5">
                                    <div class="form-group">
                                        <div class="col-md-4">
                                            <nop-label asp-for="SearchFriendlyName" />
                                        </div>
                                        <div class="col-md-8">
                                            <nop-editor asp-for="SearchFriendlyName" />
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <div class="col-md-4">
                                            <nop-label asp-for="SearchGroup" />
                                        </div>
                                        <div class="col-md-8">
                                            <nop-select asp-for="SearchGroup" asp-items="Model.AvailableGroups" />
                                        </div>
                                    </div>
                                </div>
                                <div class="col-md-7">
                                    <div class="form-group">
                                        <div class="col-md-4">
                                            <nop-label asp-for="SearchLoadModeId" />
                                        </div>
                                        <div class="col-md-8">
                                            <nop-select asp-for="SearchLoadModeId" asp-items="Model.AvailableLoadModes" />
                                        </div>
                                    </div>
                                </div>
                                <div class="col-md-7">
                                    <div class="form-group">
                                        <div class="col-md-4">
                                            <nop-label asp-for="SearchAuthor" />
                                        </div>
                                        <div class="col-md-8">
                                            <nop-editor asp-for="SearchAuthor" />
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-md-7 col-md-offset-5">
                                    <button type="button" id="search-plugins-local" class="btn btn-primary btn-search">
                                        <i class="fa fa-search"></i>
                                        @T("Admin.Common.Search")
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="panel panel-default">
                    <div class="panel-body">
                        @await Html.PartialAsync("Table", new DataTablesModel
                        {
                            Name = "plugins-local-grid",
                            UrlRead = new DataUrl("ListSelect", "Plugin", null),
                            SearchButtonId = "search-plugins-local",
                            Length = Model.PageSize,
                            LengthMenu = Model.AvailablePageSizes,
                            Filters = new List<FilterParameter>
                            {
                                new FilterParameter(nameof(Model.SearchLoadModeId)),
                                 new FilterParameter(nameof(Model.SearchFriendlyName)),
                                new FilterParameter(nameof(Model.SearchGroup)),
                                new FilterParameter(nameof(Model.SearchAuthor))
                            },
                            ColumnCollection = new List<ColumnProperty>
                            {
                                new ColumnProperty(nameof(PluginModel.Group))
                                {
                                    Title = T("Admin.Configuration.Plugins.Fields.Group").Text,
                                    Width = "150"
                                },
                                new ColumnProperty(nameof(PluginModel.LogoUrl))
                                {
                                    Title = T("Admin.Configuration.Plugins.Fields.Logo").Text,
                                    Width = "200",
                                    ClassName = NopColumnClassDefaults.CenterAll,
                                    Render = new RenderPicture()
                                },
                                new ColumnProperty(nameof(PluginModel.Description))
                                {
                                    Title = T("Admin.Configuration.Plugins.Info").Text,
                                    Width = "400",
                                    Render = new RenderCustom("renderColumnDescription")
                                },
                                new ColumnProperty(nameof(PluginModel.FriendlyName))
                                {
                                    Title = T("Admin.Configuration.Plugins.AdditionalInfo").Text,
                                    Render = new RenderCustom("renderColumnFriendlyName")
                                },
                                new ColumnProperty(nameof(PluginModel.Installed))
                                {
                                    Title = T("Admin.Configuration.Plugins.Fields.Installation").Text,
                                    Width = "100",
                                    ClassName =  NopColumnClassDefaults.Button,
                                    Render = new RenderCustom("renderColumnInstalled")
                                }
                            }
                        })
                        
                        <script>
                            var textRenderer = $.fn.dataTable.render.text().display;

                            function renderColumnDescription(data, type, row, meta) {
                                var res = '<b>' + textRenderer(row.FriendlyName) + '</b>';

                                if (row.Description) {
                                    res = res + '<br />' + textRenderer(row.Description);
                                }
                                res = res + '<br />';
                                //allow configuration only when a plugin has a configuration URL specified
                                if (row.ConfigurationUrl && row.ConfigurationUrl.length > 0) {
                                    res = res + '<a class="btn btn-default margin-r-5" href="' + row.ConfigurationUrl + '"><i class="fa fa-cog"></i>@T("Admin.Configuration.Plugins.Fields.Configure")</a>';
                                }
                                if (row.Installed) {
                                    res = res + '<a class="btn btn-default" href="javascript:OpenWindow(\'@Url.Content("~/Admin/Plugin/EditPopup")?systemName=' + row.SystemName + '&btnId=btnRefreshList&formId=plugins-form-local\', 800, 500, true);"><i class="fa fa-pencil"></i>@T("Admin.Common.Edit")</a>';
                                }

                                return res;
                            }

                            function renderColumnFriendlyName(data, type, row, meta) {
                                var isInstall = '<li>@T("Admin.Configuration.Plugins.Fields.Installed"): ';
                                if (row.Installed) {
                                    isInstall = isInstall + '<i class="fa fa-check true-icon"></i></li>';
                                }
                                else {
                                    isInstall = isInstall + '<i class="fa fa-close false-icon"></i></li>';
                                }

                                var canChange = '';
                                if (row.CanChangeEnabled) {
                                    canChange = canChange + '<li>@T("Admin.Configuration.Plugins.Fields.IsEnabled"): ';
                                    if (row.IsEnabled) {
                                        canChange = canChange + '<i class="fa fa-check true-icon"></i>';
                                    }
                                    else {
                                        canChange = canChange + '<i class="fa fa-close false-icon"></i>';
                                    };   
                                    canChange = canChange + '</li>';
                                };

                                return '<ul class="common-list">' +
                                    '<li>@T("Admin.Configuration.Plugins.Fields.Version"): ' +  row.Version + '</li>' +
                                    '<li>@T("Admin.Configuration.Plugins.Fields.Author"): ' + row.Author + '</li>' +
                                    '<li>@T("Admin.Configuration.Plugins.Fields.SystemName"): ' + row.SystemName + '</li>' +
                                    '<li>@T("Admin.Configuration.Plugins.Fields.DisplayOrder"): ' + row.DisplayOrder + '</li>' +
                                    isInstall + 
                                    canChange;
                            }

                            function renderColumnInstalled(data, type, row, meta) {
                                if (row.Installed) {
                                    return '<button type="submit" name="uninstall-plugin-link-' + row.SystemName + '" class="btn bg-red uninstall-plugin-link">@T("Admin.Configuration.Plugins.Fields.Uninstall")</button>';
                                } else {
                                    return '<button type="submit" name="install-plugin-link-' + row.SystemName + '" class="btn bg-green install-plugin-link">@T("Admin.Configuration.Plugins.Fields.Install")</button> <button type="button" name="delete-plugin-link-' + row.SystemName + '" class="btn bg-red delete-plugin-link" >@T("Admin.Configuration.Plugins.Fields.Delete")</button>';
                                }
                            }
                        </script>
                    </div>

                    <div id="plugin-delete-confirmation" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="plugin-delete-confirmation-title" style="display: none;">
                        <div class="modal-dialog">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
                                    <h4 class="modal-title" id="plugindelete-confirmation-title">@T("Admin.Common.AreYouSure")</h4>
                                </div>
                                <div class="form-horizontal">
                                    <div class="modal-body">
                                        @T("Admin.Common.DeleteConfirmation")
                                    </div>
                                    <div class="modal-footer">
                                        <span class="btn btn-default" data-dismiss="modal">@T("Admin.Common.NoCancel")</span>
                                        <button type="submit" class="btn bg-red pull-right">@T("Admin.Common.Delete")</button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div>
                        <input type="submit" id="btnRefreshList" style="display: none" />
                        <script>
                            $(document).ready(function () {
                                $('#btnRefreshList').click(function () {
                                    //refresh grid
                                    updateTable('#plugins-local-grid');

                                    //return false to don't reload a page
                                    return false;
                                });
                            });
                        </script>
                    </div>
                </div>
            </div>
        </div>
    </div>

    @await Component.InvokeAsync("AdminWidget", new { widgetZone = AdminWidgetZones.PluginDetailsBlock, additionalData = Model })
</form>

@*upload plugin form*@
<div id="uploadplugin-window" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="uploadplugin-window-title">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="uploadplugin-window-title">@T("Admin.Configuration.Plugins.Upload")</h4>
            </div>
            <form asp-controller="Plugin" asp-action="UploadPluginsAndThemes" method="post" enctype="multipart/form-data">
                <div class="form-horizontal">
                    <div class="modal-body">
                        <ul class="common-list">
                            <li>
                                @T("Admin.Configuration.Plugins.Upload.Hint1")
                            </li>
                            <li>
                                @string.Format(T("Admin.Configuration.Plugins.Upload.Hint2").Text, NopPluginDefaults.UploadedItemsFileName)
                            </li>
                            <li>
                                <em>@T("Admin.Configuration.Plugins.Upload.Hint3")</em>
                            </li>
                            <li>
                                <em>@T("Admin.Configuration.Plugins.Upload.Hint4")</em>
                            </li>
                        </ul>
                        <div class="form-group">
                            <div class="col-md-2">
                                <div class="label-wrapper">
                                    <label class="control-label">
                                        @T("Admin.Common.ZipFile")
                                    </label>
                                </div>
                            </div>
                            <div class="col-md-10">
                                <input type="file" id="archivefile" name="archivefile" class="form-control" />
                            </div>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="submit" class="btn btn-primary" id="upload-plugin">
                            @T("Admin.Configuration.Plugins.Upload")
                        </button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>


